﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.ComponentModel;

namespace DAO
{
    public  class CategoryDAO
    {
        public CategoryDTO CreateCategoryDTOFromEntity(TheLoai theLoai)
        {
            CategoryDTO cateDto = new CategoryDTO { 
                CategoryCode = theLoai.MaTheLoai,
                CategoryID = theLoai.TheLoaiID,
                CategoryName = theLoai.TenTheLoai,
                Status = bool.Parse( theLoai.SuDung.ToString())
            };
            return cateDto;
        }
        public int InsertCategory(CategoryDTO categoryDto)
        {
            using(BookStoreEntities ent = new BookStoreEntities())
            {
                TheLoai categoryInt = new TheLoai
                {
                    MaTheLoai = categoryDto.CategoryCode,
                    TenTheLoai = categoryDto.CategoryName,
                    SuDung = true
                };

                if (ent.TheLoais.Count(t => t.TheLoaiID != null) > 0)
                {
                    categoryInt.TheLoaiID = ent.TheLoais.Max(t => t.TheLoaiID) + 1;
                }
                else
                {
                    categoryInt.TheLoaiID = 1;
                }

                ent.AddToTheLoais(categoryInt);

                DocSequenceDAO docSeqDAO = new DocSequenceDAO();
                docSeqDAO.UpdateNextDocSequence(DocSequence.Category.ToString());

                ent.SaveChanges();

                return categoryInt.TheLoaiID;
            }
        }
        public List<CategoryDTO> GetCategoryList()
        {
            List<CategoryDTO> listCategoryDto = new List<CategoryDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<TheLoai> listTheLoai = ent.TheLoais.Where(c => c.SuDung == true).ToList();
                foreach (TheLoai item in listTheLoai)
                {
                    CategoryDTO cateDto = CreateCategoryDTOFromEntity(item);
                    listCategoryDto.Add(cateDto);
                }
            }
            return listCategoryDto;
        }

        public BindingList<CategoryDTO> GetNewBindingListCategory()
        {
            BindingList<CategoryDTO> listCategoryDto = new BindingList<CategoryDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<TheLoai> listTheLoai = ent.TheLoais.Where(c => c.SuDung == true).ToList();
                foreach (TheLoai item in listTheLoai)
                {
                    CategoryDTO cateDto = CreateCategoryDTOFromEntity(item);
                    listCategoryDto.Add(cateDto);
                }
            }
            return listCategoryDto;
        }

        public bool UpdateCategory(CategoryDTO cateDTO)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    TheLoai cateEnt = ent.TheLoais.FirstOrDefault(c => c.TheLoaiID == cateDTO.CategoryID);

                    cateEnt.TenTheLoai = cateDTO.CategoryName;

                    ent.SaveChanges();

                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public CategoryDTO GetCategoryByID(int cateID)
        {
            try
            {
                using(BookStoreEntities ent = new BookStoreEntities())
                {
                    TheLoai cate = ent.TheLoais.FirstOrDefault(c => c.TheLoaiID == cateID);

                    CategoryDTO cateDTO = CreateCategoryDTOFromEntity(cate);

                    return cateDTO;
                }
            }
            catch
            {
                return null;
            }
        }

        public bool DeleteCategory(int cateID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    TheLoai cateEnt = ent.TheLoais.FirstOrDefault(c => c.TheLoaiID == cateID);
                    cateEnt.SuDung = false;
                    ent.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool CheckExistsCode(string code)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    int count = ent.TheLoais.Where(c => c.MaTheLoai == code).Count();

                    if (count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch
            {
                return true;
            }
        }
    }
}
